﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBModel;
using DBModel.DAL;

namespace CV_Projekt
{
    public partial class RegisterCV : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            ResetLabels();

            if (!IsPostBack)
            {
                var user = Session["User"] as Applicant;

                if (user == null)
                {
                    Response.Redirect("Default.aspx");
                }

                TextBoxFirstName.Text = user.FirstName;
                TextBoxLastName.Text = user.LastName;
                TextBoxSocialSecurityNumber.Text = user.SocialSecurityNumber;
                TextBoxAddress.Text = user.Street;
                TextBoxPostalCode.Text = user.PostalCode;
                TextBoxCity.Text = user.City;
                TextBoxLand.Text = user.Country;
                TextBoxPhoneNumber.Text = user.Phone;
                TextBoxEmail.Text = user.Email;

                CurriculumVitae cv = (CurriculumVitae)Session["CV"];
                LoadAllEnums();
                LoadAllCVProperties();
            }
        }

        private void ResetLabels()
        {
            LabelUserInfoChanged.Text = "";
            LabelConfirmationAddPresentation.Text = "";
            LabelConfirmationAddEmployment.Text = "";
            LabelConfirmationAddEducation.Text = "";
            LabelConfirmationAddSkill.Text = "";
            LabelConfirmationAddOtherQualification.Text = "";
            LabelConfirmationAddLanguage.Text = "";
        }
        private void LoadAllCVProperties()
        {
            TextBoxPresentation.Text = ((CurriculumVitae)Session["CV"]).Presentation;
            LoadEmployments();
            LoadEducations();
            LoadSkills();
            LoadOtherQualifications();
            LoadLanguages();
        }
        private void LoadAllEnums()
        {
            LoadEducationEnums();
            LoadSkillEnums();
            LoadLanguageEnums();
        }
        private void LoadEducationEnums()
        {
            DropDownListEducation.DataSource = Enum.GetNames(typeof(EducationDegree)).ToList();
            DropDownListEducation.DataBind();
        }
        private void LoadSkillEnums()
        {
            DropDownListSkills.DataSource = Enum.GetNames(typeof(Grade)).ToList();
            DropDownListSkills.DataBind();
        }
        private void LoadLanguageEnums()
        {
            DropDownListLanguage.DataSource = Enum.GetNames(typeof(Grade)).ToList();
            DropDownListLanguage.DataBind();
        }
        private void LoadEmployments()
        {
            DropDownListExistingEmployments.DataSource =
                ((CurriculumVitae)Session["CV"]).Employment.Where(employment => !employment.IsDeleted).ToList();
            DropDownListExistingEmployments.DataBind();
        }
        private void LoadEducations()
        {
            DropDownListExistingEducations.DataSource = ((CurriculumVitae)Session["CV"]).Education.Where(e => !e.IsDeleted).ToList();
            DropDownListExistingEducations.DataBind();
        }
        private void LoadSkills()
        {
            DropDownListExistingSkills.DataSource = ((CurriculumVitae)Session["CV"]).Skill.Where(s => !s.IsDeleted).ToList();
            DropDownListExistingSkills.DataBind();
        }
        private void LoadOtherQualifications()
        {
            DropDownListExistingOtherQualifications.DataSource =
                ((CurriculumVitae)Session["CV"]).OtherQualification.Where(o => !o.IsDeleted).ToList();
            DropDownListExistingOtherQualifications.DataBind();
        }
        private void LoadLanguages()
        {
            DropDownListExistingLanguages.DataSource = ((CurriculumVitae)Session["CV"]).Language.Where(l => !l.IsDeleted).ToList();
            DropDownListExistingLanguages.DataBind();
        }

        //protected void ButtonSaveKontaktuppgifter_Click(object sender, EventArgs e)
        //{
        //    var user = new Applicant();
        //    user.FirstName = TextBoxFirstName.Text;
        //    user.LastName = TextBoxLastName.Text;
        //    user.SocialSecurityNumber = TextBoxSocialSecurityNumber.Text;
        //    user.Street = TextBoxAddress.Text;
        //    user.PostalCode = TextBoxPostalCode.Text;
        //    user.City = TextBoxCity.Text;
        //    user.Phone = TextBoxPhoneNumber.Text;
        //    user.Email = TextBoxEmail.Text;

        //    //DB.Context.Users.Add(user);
        //    //DB.Context.SaveChanges();
        //}

        protected void ButtonAddEmployment_Click(object sender, EventArgs e)
        {
            var cv = Session["CV"] as CurriculumVitae;

            Employment employment = new Employment();
            employment.CompanyName = TextBoxCompanyName.Text;
            employment.Titel = TextBoxEmploymentTitle.Text;
            employment.StartDate = CalendarStartEmployment.SelectedDate;
            employment.EndDate = CalendarStopEmployment.SelectedDate;
            employment.WorkDescription = TextBoxDescriptionEmployment.Text;
            cv.Employment.Add(employment);
            DB.Context.SaveChanges();
            Session["CV"] = cv;
            TextBoxCompanyName.Text = "";
            TextBoxEmploymentTitle.Text = "";
            CalendarStartEmployment.SelectedDate = DateTime.Now;
            CalendarStopEmployment.SelectedDate = DateTime.Now;
            TextBoxDescriptionEmployment.Text = "";
            LabelConfirmationAddEmployment.Text = "Anställning är upplagd!";
            LoadEmployments();
        }

        protected void ButtonAddEducation_Click(object sender, EventArgs e)
        {
            var cv = Session["CV"] as CurriculumVitae;

            Education education = new Education();
            education.EducationName = TextBoxEducationName.Text;
            education.SiteOfEducation = TextBoxEducationLocation.Text;
            education.StartDate = CalendarEducationStart.SelectedDate;
            education.EndDate = CalendarEducationStop.SelectedDate;
            education.EducationDescription = TextBoxDescriptionEducation.Text;
            education.Degree =
                (EducationDegree)Enum.Parse(typeof(EducationDegree), DropDownListEducation.SelectedItem.Text);
            cv.Education.Add(education);
            DB.Context.SaveChanges();
            Session["CV"] = cv;
            TextBoxEducationName.Text = "";
            TextBoxEducationLocation.Text = "";
            CalendarEducationStart.SelectedDate = DateTime.Now;
            CalendarEducationStop.SelectedDate = DateTime.Now;
            TextBoxDescriptionEducation.Text = "";
            LabelConfirmationAddEducation.Text = "Utbildning är upplagd!";
            LoadEducations();
        }

        protected void ButtonSaveSkills_Click(object sender, EventArgs e)
        {
            var cv = Session["CV"] as CurriculumVitae;

            Skill skill = new Skill();
            skill.SkillName = TextBoxSkillsTitle.Text;
            skill.SkillGrade = (Grade)Enum.Parse(typeof(Grade), DropDownListSkills.SelectedItem.Text);
            skill.SkillYearsOfExperience = int.Parse(TextBoxYearsOfSkill.Text);
            skill.SkillDescription = TextBoxDescriptionSkills.Text;
            cv.Skill.Add(skill);
            DB.Context.SaveChanges();
            Session["CV"] = cv;
            TextBoxSkillsTitle.Text = "";
            TextBoxYearsOfSkill.Text = "";
            TextBoxDescriptionSkills.Text = "";
            LabelConfirmationAddSkill.Text = "Erfarenhet är upplagd!";
            LoadSkills();
        }

        protected void ButtonSaveOtherQualification_Click(object sender, EventArgs e)
        {
            var cv = Session["CV"] as CurriculumVitae;

            OtherQualification otherQualification = new OtherQualification();
            otherQualification.Title = TextBoxOtherQualificationTitle.Text;
            otherQualification.Organizer = TextBoxOtherQualificationOrganizer.Text;
            otherQualification.StartDate = CalendarOtherQualificationStart.SelectedDate;
            otherQualification.EndDate = CalendarOtherQualificationStop.SelectedDate;
            otherQualification.QualificationDescription = TextBoxDescriptionOtherQualifications.Text;
            cv.OtherQualification.Add(otherQualification);
            DB.Context.SaveChanges();
            Session["CV"] = cv;
            TextBoxOtherQualificationTitle.Text = "";
            TextBoxOtherQualificationOrganizer.Text = "";
            CalendarOtherQualificationStart.SelectedDate = DateTime.Now;
            CalendarOtherQualificationStop.SelectedDate = DateTime.Now;
            TextBoxDescriptionOtherQualifications.Text = "";
            LabelConfirmationAddOtherQualification.Text = "Merit är upplagd!";
            LoadOtherQualifications();
        }

        protected void ButtonDeleteCV_Click(object sender, EventArgs e)
        {
            //var cv = Session["CV"] as CurriculumVitae;
            //cv.IsDeleted = true;
            //DB.Context.SaveChanges();
        }

        protected void ButtonSaveCV_Click(object sender, EventArgs e)
        {
            DB.Context.SaveChanges();
            LabelConfirmationAddCV.Text = "CV är upplagt!";
        }

        protected void ButtonAddLanguage_Click(object sender, EventArgs e)
        {
            var cv = Session["CV"] as CurriculumVitae;

            Language language = new Language();
            language.LanguageName = TextBoxLanguageName.Text;
            language.LanguageGrade = (Grade)Enum.Parse(typeof(Grade), DropDownListLanguage.SelectedItem.Text);
            language.LanguageYearsOfExperience = int.Parse(TextBoxLanguageYearsOfExperience.Text);
            cv.Language.Add(language);
            DB.Context.SaveChanges();
            Session["CV"] = cv;
            TextBoxLanguageName.Text = "";
            TextBoxLanguageYearsOfExperience.Text = "";
            LabelConfirmationAddLanguage.Text = "Språk är upplagt!";
            LoadLanguages();
        }

        protected void ButtonChangeContactInfo_Click(object sender, EventArgs e)
        {
            Applicant applicant = Session["User"] as Applicant;
            if (applicant != null)
            {
                applicant.FirstName = TextBoxFirstName.Text.Trim();
                applicant.LastName = TextBoxLastName.Text.Trim();
                applicant.SocialSecurityNumber = TextBoxSocialSecurityNumber.Text.Trim();
                applicant.Street = TextBoxAddress.Text.Trim();
                applicant.PostalCode = TextBoxPostalCode.Text.Trim();
                applicant.City = TextBoxCity.Text.Trim();
                applicant.Country = TextBoxLand.Text.Trim();
                applicant.Phone = TextBoxPhoneNumber.Text.Trim();
                applicant.Email = TextBoxEmail.Text.Trim();
                DB.Context.SaveChanges();
                LabelUserInfoChanged.Text = "Användarinfo sparad!";
            }
        }

        protected void ButtonAddPresentation0_Click(object sender, EventArgs e)
        {
            ((CurriculumVitae)Session["CV"]).Presentation = TextBoxPresentation.Text;
            DB.Context.SaveChanges();
            LabelConfirmationAddPresentation.Text = "Presentation sparad!";
        }

        protected void ButtonDeleteEmployment_Click(object sender, EventArgs e)
        {
            string[] employmentStrings = DropDownListExistingEmployments.SelectedItem.Value.Split(',');
            int id = int.Parse(employmentStrings[0]);
            EmploymentDB.DeleteEmploymentById(id);
            LoadEmployments();
        }

        protected void ButtonDeleteEducation_Click(object sender, EventArgs e)
        {
            string[] educationStrings = DropDownListExistingEducations.SelectedItem.Value.Split(',');
            int id = int.Parse(educationStrings[0]);
            EducationDB.DeleteEducationById(id);
            LoadEducations();
        }

        protected void ButtonDeleteSkill_Click(object sender, EventArgs e)
        {
            string[] skillStrings = DropDownListExistingSkills.SelectedItem.Value.Split(',');
            int id = int.Parse(skillStrings[0]);
            SkillDB.DeleteSkillById(id);
            LoadSkills();
        }

        protected void ButtonDeleteOtherQualification_Click(object sender, EventArgs e)
        {
            string[] skillStrings = DropDownListExistingOtherQualifications.SelectedItem.Value.Split(',');
            int id = int.Parse(skillStrings[0]);
            OtherQualificationDB.DeleteOterQualificationById(id);
            LoadOtherQualifications();
        }

        protected void ButtonDeleteLanguage_Click(object sender, EventArgs e)
        {
            string[] languageStrings = DropDownListExistingLanguages.SelectedItem.Value.Split(',');
            int id = int.Parse(languageStrings[0]);
            LanguageDB.DeleteLanguageById(id);
            LoadLanguages();
        }
    }
}